home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 16 windows forms / windowsformsdemo / menuform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  16.7 KB  |  434 lines

  1. Public Class MenuForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.  
  26.     Friend WithEvents ctxTextBox As System.Windows.Forms.ContextMenu
  27.     Friend WithEvents mnuClearAll As System.Windows.Forms.MenuItem
  28.     Friend WithEvents mnuSelectAll As System.Windows.Forms.MenuItem
  29.     Friend WithEvents mnuTodaysDate As System.Windows.Forms.MenuItem
  30.  
  31.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  32.     Friend WithEvents lblStatus As System.Windows.Forms.Label
  33.  
  34.     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  35.  
  36.     Friend WithEvents MenuItem17 As System.Windows.Forms.MenuItem
  37.     Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem
  38.     Friend WithEvents mnuEditSep2 As System.Windows.Forms.MenuItem
  39.     Friend WithEvents mnuEditSelectAll As System.Windows.Forms.MenuItem
  40.     Friend WithEvents mnuHelp As System.Windows.Forms.MenuItem
  41.     Friend WithEvents mnuHelpSep As System.Windows.Forms.MenuItem
  42.     Friend WithEvents mnuEditSep As System.Windows.Forms.MenuItem
  43.     Friend WithEvents mnuEditCopy As System.Windows.Forms.MenuItem
  44.     Friend WithEvents mnuEditCut As System.Windows.Forms.MenuItem
  45.     Friend WithEvents mnuEdit As System.Windows.Forms.MenuItem
  46.     Friend WithEvents mnuEditUndo As System.Windows.Forms.MenuItem
  47.     Friend WithEvents mnuEditPaste As System.Windows.Forms.MenuItem
  48.     Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
  49.     Friend WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem
  50.     Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
  51.     Friend WithEvents mnuFileSep As System.Windows.Forms.MenuItem
  52.     Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
  53.     Friend WithEvents mnuFileNew As System.Windows.Forms.MenuItem
  54.     Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
  55.     Friend WithEvents mnuFileNewDataFile As System.Windows.Forms.MenuItem
  56.     Friend WithEvents mnuFileNewReport As System.Windows.Forms.MenuItem
  57.     Friend WithEvents mnuFileNewFolder As System.Windows.Forms.MenuItem
  58.     Friend WithEvents mnuPopupSep As System.Windows.Forms.MenuItem
  59.     Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  60.     Friend WithEvents btnCreatePopupMenu As System.Windows.Forms.Button
  61.     
  62.  
  63.     'Required by the Windows Form Designer
  64.     Private components As System.ComponentModel.Container
  65.  
  66.     'NOTE: The following procedure is required by the Windows Form Designer
  67.     'It can be modified using the Windows Form Designer.  
  68.     'Do not modify it using the code editor.
  69.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  70.         Me.MenuItem17 = New System.Windows.Forms.MenuItem()
  71.         Me.mnuFileNewFolder = New System.Windows.Forms.MenuItem()
  72.         Me.MenuItem19 = New System.Windows.Forms.MenuItem()
  73.         Me.TextBox2 = New System.Windows.Forms.TextBox()
  74.         Me.mnuFileSave = New System.Windows.Forms.MenuItem()
  75.         Me.TextBox1 = New System.Windows.Forms.TextBox()
  76.         Me.ctxTextBox = New System.Windows.Forms.ContextMenu()
  77.         Me.mnuClearAll = New System.Windows.Forms.MenuItem()
  78.         Me.mnuSelectAll = New System.Windows.Forms.MenuItem()
  79.         Me.mnuPopupSep = New System.Windows.Forms.MenuItem()
  80.         Me.mnuTodaysDate = New System.Windows.Forms.MenuItem()
  81.         Me.mnuHelp = New System.Windows.Forms.MenuItem()
  82.         Me.mnuHelpSep = New System.Windows.Forms.MenuItem()
  83.         Me.mnuFileNew = New System.Windows.Forms.MenuItem()
  84.         Me.mnuFileNewDataFile = New System.Windows.Forms.MenuItem()
  85.         Me.mnuFileNewReport = New System.Windows.Forms.MenuItem()
  86.         Me.mnuFileExit = New System.Windows.Forms.MenuItem()
  87.         Me.mnuEditCopy = New System.Windows.Forms.MenuItem()
  88.         Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
  89.         Me.mnuEditUndo = New System.Windows.Forms.MenuItem()
  90.         Me.MainMenu1 = New System.Windows.Forms.MainMenu()
  91.         Me.mnuFile = New System.Windows.Forms.MenuItem()
  92.         Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem()
  93.         Me.mnuFileSep = New System.Windows.Forms.MenuItem()
  94.         Me.mnuEdit = New System.Windows.Forms.MenuItem()
  95.         Me.mnuEditSep = New System.Windows.Forms.MenuItem()
  96.         Me.mnuEditCut = New System.Windows.Forms.MenuItem()
  97.         Me.mnuEditPaste = New System.Windows.Forms.MenuItem()
  98.         Me.mnuEditSep2 = New System.Windows.Forms.MenuItem()
  99.         Me.mnuEditSelectAll = New System.Windows.Forms.MenuItem()
  100.         Me.lblStatus = New System.Windows.Forms.Label()
  101.         Me.btnCreatePopupMenu = New System.Windows.Forms.Button()
  102.         Me.SuspendLayout()
  103.         '
  104.         'MenuItem17
  105.         '
  106.         Me.MenuItem17.Index = 0
  107.         Me.MenuItem17.Text = "&Index"
  108.         '
  109.         'mnuFileNewFolder
  110.         '
  111.         Me.mnuFileNewFolder.Index = 2
  112.         Me.mnuFileNewFolder.Text = "&Folder"
  113.         '
  114.         'MenuItem19
  115.         '
  116.         Me.MenuItem19.Index = 2
  117.         Me.MenuItem19.Text = "&About"
  118.         '
  119.         'TextBox2
  120.         '
  121.         Me.TextBox2.Location = New System.Drawing.Point(48, 120)
  122.         Me.TextBox2.Name = "TextBox2"
  123.         Me.TextBox2.Size = New System.Drawing.Size(384, 20)
  124.         Me.TextBox2.TabIndex = 3
  125.         Me.TextBox2.Text = ""
  126.         '
  127.         'mnuFileSave
  128.         '
  129.         Me.mnuFileSave.Index = 2
  130.         Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
  131.         Me.mnuFileSave.Text = "&Save"
  132.         '
  133.         'TextBox1
  134.         '
  135.         Me.TextBox1.ContextMenu = Me.ctxTextBox
  136.         Me.TextBox1.Location = New System.Drawing.Point(48, 72)
  137.         Me.TextBox1.Name = "TextBox1"
  138.         Me.TextBox1.Size = New System.Drawing.Size(384, 20)
  139.         Me.TextBox1.TabIndex = 0
  140.         Me.TextBox1.Text = "Right-click to display context menu"
  141.         '
  142.         'ctxTextBox
  143.         '
  144.         Me.ctxTextBox.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuClearAll, Me.mnuSelectAll, Me.mnuPopupSep, Me.mnuTodaysDate})
  145.         '
  146.         'mnuClearAll
  147.         '
  148.         Me.mnuClearAll.Index = 0
  149.         Me.mnuClearAll.Text = "&Clear All"
  150.         '
  151.         'mnuSelectAll
  152.         '
  153.         Me.mnuSelectAll.Index = 1
  154.         Me.mnuSelectAll.Text = "&Select All"
  155.         '
  156.         'mnuPopupSep
  157.         '
  158.         Me.mnuPopupSep.Index = 2
  159.         Me.mnuPopupSep.Text = "-"
  160.         '
  161.         'mnuTodaysDate
  162.         '
  163.         Me.mnuTodaysDate.Index = 3
  164.         Me.mnuTodaysDate.Text = "Insert &Today's Date"
  165.         '
  166.         'mnuHelp
  167.         '
  168.         Me.mnuHelp.Index = 2
  169.         Me.mnuHelp.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem17, Me.mnuHelpSep, Me.MenuItem19})
  170.         Me.mnuHelp.MergeOrder = 9
  171.         Me.mnuHelp.Text = "&Help"
  172.         '
  173.         'mnuHelpSep
  174.         '
  175.         Me.mnuHelpSep.Index = 1
  176.         Me.mnuHelpSep.Text = "-"
  177.         '
  178.         'mnuFileNew
  179.         '
  180.         Me.mnuFileNew.Index = 0
  181.         Me.mnuFileNew.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNewDataFile, Me.mnuFileNewReport, Me.mnuFileNewFolder})
  182.         Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
  183.         Me.mnuFileNew.Text = "&New"
  184.         '
  185.         'mnuFileNewDataFile
  186.         '
  187.         Me.mnuFileNewDataFile.Index = 0
  188.         Me.mnuFileNewDataFile.Text = "&Data File"
  189.         '
  190.         'mnuFileNewReport
  191.         '
  192.         Me.mnuFileNewReport.Index = 1
  193.         Me.mnuFileNewReport.Text = "&Report"
  194.         '
  195.         'mnuFileExit
  196.         '
  197.         Me.mnuFileExit.Index = 5
  198.         Me.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlX
  199.         Me.mnuFileExit.Text = "E&xit"
  200.         '
  201.         'mnuEditCopy
  202.         '
  203.         Me.mnuEditCopy.Index = 3
  204.         Me.mnuEditCopy.Text = "&Copy"
  205.         '
  206.         'mnuFileOpen
  207.         '
  208.         Me.mnuFileOpen.Index = 1
  209.         Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
  210.         Me.mnuFileOpen.Text = "&Open"
  211.         '
  212.         'mnuEditUndo
  213.         '
  214.         Me.mnuEditUndo.Index = 0
  215.         Me.mnuEditUndo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ
  216.         Me.mnuEditUndo.Text = "&Undo"
  217.         '
  218.         'MainMenu1
  219.         '
  220.         Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.mnuHelp})
  221.         '
  222.         'mnuFile
  223.         '
  224.         Me.mnuFile.Index = 0
  225.         Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.mnuFileOpen, Me.mnuFileSave, Me.mnuFileSaveAs, Me.mnuFileSep, Me.mnuFileExit})
  226.         Me.mnuFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems
  227.         Me.mnuFile.Text = "&File"
  228.         '
  229.         'mnuFileSaveAs
  230.         '
  231.         Me.mnuFileSaveAs.Index = 3
  232.         Me.mnuFileSaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlA
  233.         Me.mnuFileSaveAs.Text = "Save &As"
  234.         '
  235.         'mnuFileSep
  236.         '
  237.         Me.mnuFileSep.Index = 4
  238.         Me.mnuFileSep.Text = "-"
  239.         '
  240.         'mnuEdit
  241.         '
  242.         Me.mnuEdit.Index = 1
  243.         Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditUndo, Me.mnuEditSep, Me.mnuEditCut, Me.mnuEditCopy, Me.mnuEditPaste, Me.mnuEditSep2, Me.mnuEditSelectAll})
  244.         Me.mnuEdit.MergeOrder = 1
  245.         Me.mnuEdit.Text = "&Edit"
  246.         '
  247.         'mnuEditSep
  248.         '
  249.         Me.mnuEditSep.Index = 1
  250.         Me.mnuEditSep.Text = "-"
  251.         '
  252.         'mnuEditCut
  253.         '
  254.         Me.mnuEditCut.Index = 2
  255.         Me.mnuEditCut.Text = "&Cut"
  256.         '
  257.         'mnuEditPaste
  258.         '
  259.         Me.mnuEditPaste.Index = 4
  260.         Me.mnuEditPaste.Text = "&Paste"
  261.         '
  262.         'mnuEditSep2
  263.         '
  264.         Me.mnuEditSep2.Index = 5
  265.         Me.mnuEditSep2.Text = "-"
  266.         '
  267.         'mnuEditSelectAll
  268.         '
  269.         Me.mnuEditSelectAll.Index = 6
  270.         Me.mnuEditSelectAll.Text = "&Select All"
  271.         '
  272.         'lblStatus
  273.         '
  274.         Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  275.         Me.lblStatus.Dock = System.Windows.Forms.DockStyle.Bottom
  276.         Me.lblStatus.Location = New System.Drawing.Point(0, 217)
  277.         Me.lblStatus.Name = "lblStatus"
  278.         Me.lblStatus.Size = New System.Drawing.Size(480, 24)
  279.         Me.lblStatus.TabIndex = 1
  280.         '
  281.         'btnCreatePopupMenu
  282.         '
  283.         Me.btnCreatePopupMenu.Location = New System.Drawing.Point(48, 152)
  284.         Me.btnCreatePopupMenu.Name = "btnCreatePopupMenu"
  285.         Me.btnCreatePopupMenu.Size = New System.Drawing.Size(384, 24)
  286.         Me.btnCreatePopupMenu.TabIndex = 2
  287.         Me.btnCreatePopupMenu.Text = "Create Popup Menu for this TextBox"
  288.         '
  289.         'MenuForm
  290.         '
  291.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  292.         Me.ClientSize = New System.Drawing.Size(480, 241)
  293.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.btnCreatePopupMenu, Me.lblStatus, Me.TextBox1})
  294.         Me.Menu = Me.MainMenu1
  295.         Me.Name = "MenuForm"
  296.         Me.Text = "MenuForm"
  297.         Me.ResumeLayout(False)
  298.  
  299.     End Sub
  300.  
  301. #End Region
  302.  
  303.     Private Sub MenuForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  304.         InitializeMenuEvents(Me.Menu, AddressOf MenuItem_Click, AddressOf MenuItem_Select)
  305.     End Sub
  306.  
  307.     ' Make all the menu items in this form point to MenuItem_Click and MenuItem_Select routines.
  308.     ' The main application should call this routine with m = Me.Menu
  309.  
  310.     Sub InitializeMenuEvents(ByVal m As Menu, ByVal ClickEvent As EventHandler, ByVal SelectEvent As EventHandler)
  311.         Dim mi As MenuItem
  312.  
  313.         If TypeOf m Is MenuItem Then
  314.             ' Cast the argument to a MenuItem object.
  315.             mi = DirectCast(m, MenuItem)
  316.             ' Initialize the Select event for this menu item.
  317.             AddHandler mi.Select, SelectEvent
  318.             ' Initialize the Click event, but only if this isn't a submenu
  319.             If m.MenuItems.Count = 0 Then
  320.                 AddHandler mi.Click, ClickEvent
  321.             End If
  322.         End If
  323.  
  324.         ' Call recursively for all items in the Menuitems collection
  325.         For Each mi In m.MenuItems
  326.             InitializeMenuEvents(mi, ClickEvent, SelectEvent)
  327.         Next
  328.  
  329.     End Sub
  330.  
  331.     ' common Select  routine for all menu items
  332.  
  333.     Private Sub MenuItem_Select(ByVal sender As Object, ByVal e As System.EventArgs)
  334.         Dim mi As MenuItem = CType(sender, MenuItem)
  335.         lblStatus.Text = "Help message for " & mi.Text.ToUpper
  336.     End Sub
  337.  
  338.     '  common Click routine for all menu items
  339.  
  340.     Private Sub MenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  341.         Dim mi As MenuItem = CType(sender, MenuItem)
  342.         lblStatus.Text = mi.Text.ToUpper & " has been selected"
  343.     End Sub
  344.  
  345.     ' implement a Clear command in any control that has the focus
  346.  
  347.     Private Sub mnuClearAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuClearAll.Click
  348.         ' get a reference to parent control
  349.         Dim tb As TextBox = GetSourceTextBox(sender)
  350.         tb.Text = ""
  351.     End Sub
  352.  
  353.     Private Sub mnuTodaysDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTodaysDate.Click
  354.         ' get a reference to parent control - no need for intermediate variable
  355.         GetSourceTextBox(sender).SelectedText = Now.Today.ToString
  356.     End Sub
  357.  
  358.     ' select the contents of current TextBox control
  359.  
  360.     Private Sub mnuSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSelectAll.Click
  361.         ' get a reference to parent control - no need for intermediate variable
  362.         With GetSourceTextBox(sender)
  363.             .SelectionStart = 0
  364.             .SelectionLength = 9999
  365.         End With
  366.     End Sub
  367.  
  368.     ' Get a reference to the parent control of a context menu's MenuItem
  369.     Function GetSourceTextBox(ByVal sender As Object) As TextBox
  370.         ' cast to a Menuitem object
  371.         Dim mi As MenuItem = CType(sender, MenuItem)
  372.         ' get the parent context menu
  373.         Dim ctxmenu As ContextMenu = DirectCast(mi.Parent, ContextMenu)
  374.         ' Return the source control
  375.         Return DirectCast(ctxmenu.SourceControl, TextBox)
  376.     End Function
  377.  
  378.     ' this event fires when any menu has been activated 
  379.  
  380.     Private Sub MenuForm_MenuStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MenuStart
  381.         lblStatus.Text = "Select a menu command to see its description"
  382.     End Sub
  383.  
  384.     ' this event fires when the menu has been closed (or a command has been selected)
  385.  
  386.     Private Sub MenuForm_MenuComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MenuComplete
  387.         ' Clear the Label user for help messages.
  388.         lblStatus.Text = ""
  389.     End Sub
  390.  
  391.     ' this command creates a popup menu 
  392.  
  393.     Private Sub btnCreatePopupMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreatePopupMenu.Click
  394.         CreateContextMenu()
  395.     End Sub
  396.  
  397.     ' a procedure that actually creates a context menu and
  398.     ' associates it with a TextBox 
  399.  
  400.     Sub CreateContextMenu()
  401.         Dim ctxConvertCmds As New ContextMenu()
  402.  
  403.         With ctxConvertCmds.MenuItems
  404.             .Add(New MenuItem("Clear", AddressOf ContextMenu_Click))
  405.             .Add(New MenuItem("Upper Case", AddressOf ContextMenu_Click))
  406.             .Add(New MenuItem("Lower Case", AddressOf ContextMenu_Click))
  407.         End With
  408.         TextBox2.ContextMenu = ctxConvertCmds
  409.         TextBox2.Text = "Right-click to display a popup menu"
  410.     End Sub
  411.  
  412.     ' This procedure reacts to selection in the context menu
  413.  
  414.     Private Sub ContextMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  415.         Dim mi As MenuItem = DirectCast(sender, MenuItem)
  416.  
  417.         Select Case mi.Text
  418.             Case "Clear"
  419.                 TextBox2.Text = ""
  420.             Case "Upper Case"
  421.                 TextBox2.Text = TextBox2.Text.ToUpper
  422.             Case "Lower Case"
  423.                 TextBox2.Text = TextBox2.Text.ToLower
  424.         End Select
  425.     End Sub
  426.  
  427.     ' this menu fires when the context menu is being displayed
  428.  
  429.     Private Sub ctxTextBox_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctxTextBox.Popup
  430.         lblStatus.Text = "Context menu is being displayed"
  431.     End Sub
  432.  
  433. End Class
  434.